#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>
#include <signal.h>
#include <vector>

using namespace std;
const int thread_num = 5;

class Task
{
public:
    Task(int x, int y)
        : _x(x), _y(y)
    {
    }
    ~Task() = default;
    int Excute()
    {
        return _x + _y;
    }

private:
    int _x;
    int _y;
};

class threadData
{
public:
    threadData(Task &T, const string &threadname)
        : _T(T), _threadname(threadname)
    {
    }
    ~threadData() = default;
    string retname()
    {
        return _threadname;
    }
    int excute()
    {
        return _T.Excute();
    }

private:
    string _threadname;
    Task _T;
};

class result
{
public:
    result() {}
    ~result() {}
    void setresult(int ret, string &name)
    {
        _retname = name;
        _ret = ret;
    }
    void print()
    {
        cout << _retname << ":" << _ret << endl;
    }

private:
    string _retname;
    int _ret;
};

void *thread_mission(void *args)
{
    threadData *td = static_cast<threadData *>(args);//转换成为threadData*类型
    string name = td->retname();
    result *res = new result();
    int ret = td->excute();//获取返回值
    res->setresult(ret, name);
    //获取返回值

    if(td->retname()=="Thread-2")
    {
	    cout<<"It's Thread-2 ";	 
        cout<<"It's me 9cccccccccccc"<<endl;
    }
    delete td;
    sleep(1);
    return res;
}

int main()
{
    vector<pthread_t> threads;
    for (int i = 0; i < thread_num; i++)
    {
        char threadname[64];
        snprintf(threadname, 64, "Thread-%d", i+1);
        pthread_t tid;
        Task task(10, 20);
        threadData *td = new threadData(task, threadname);//给每个线程都存进一个单独的类中
        pthread_create(&tid, nullptr, thread_mission, td);
        threads.push_back(tid);
    }
    vector<result *> res;
    void *thread_return = nullptr;

    for (auto &ch : threads)
    {
        pthread_join(ch, &thread_return);//获取返回值
        res.push_back((result *)thread_return);
    }
    for (auto &ch : res)
    {
        ch->print();
        delete ch;
    }
    return 0;
}
